﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class MaterialInventoryProcessor : BaseProcessor {

        private int _pageSize;
        private MaterialInventoryRepository _materialInventoryRepository;

        public MaterialInventoryProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._materialInventoryRepository = new MaterialInventoryRepository(pageSize);
        }

        public MaterialInventory FetchMaterialInventoryById(int materialInventoryId) {
            try {
                return this._materialInventoryRepository.FetchMaterialInventoryById(materialInventoryId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定物料盘点单失败！");
            }
        }

        public List<MaterialInventoryViewModel> FetchMaterialInventoryViewModelList(MaterialInventorySearchModel searchModel, int pageIndex = 1) {
            try {
                List<MaterialInventory> resultList = this._materialInventoryRepository.FetchMaterialInventoryList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new MaterialInventoryViewModel() {
                        Key = x.MaterialInventoryId,
                        MaterialCode = x.Material.Code,
                        MaterialName = x.Material.Name,
                        StoreBasisCode = x.StoreBasi.Code,
                        StoreBasisName = x.StoreBasi.Name,
                        PreQuantity = x.PreQuantity,
                        Quantity = x.Quantity,
                        SpanQuantity = x.SpanQuantity.Value,
                        Remarks = x.Remarks,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime
                        //,ModifiedBy = x.ModifiedBy,
                        //ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取物料盘点单列表失败！");
            }
        }

        public void Insert(MaterialInventory materialInventory) {
            try {
                materialInventory.MaterialInventoryId = Guid.NewGuid().GetHashCode();
                materialInventory.CreatedDatetime = DateTime.Now;
                materialInventory.SpanQuantity = materialInventory.Quantity - materialInventory.PreQuantity;
                this._materialInventoryRepository.InsertMaterialInventory(materialInventory);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新物料盘点单失败！");
            }
        }

        public void Update(MaterialInventory materialInventory) {
            try {
                MaterialInventory oldMaterialInventory = this.FetchMaterialInventoryById(materialInventory.MaterialInventoryId);
                if (!oldMaterialInventory.RowVersion.SequenceEqual(materialInventory.RowVersion)) {
                    throw this.BuildException("更新物料盘点单失败，数据不一致性错误！");
                }
                materialInventory.ModifiedDatetime = DateTime.Now;
                this._materialInventoryRepository.Update(materialInventory);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新物料盘点单失败！");
            }
        }


        public void Delete(MaterialInventory materialInventory) {
            try {
                this._materialInventoryRepository.Delete(materialInventory);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除物料盘点单失败！");
            }
        }

        public void Delete(params int[] materialInventoryIds) {
            try {
                this._materialInventoryRepository.DeleteMultipleMaterialInventory(materialInventoryIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除物料盘点单失败！");
            }
        }
    }
}
